﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace CastingWellEquipmentGUI.View.UserControls
{
    /// <summary>
    /// AlarmUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class AlarmUserControl : UserControl
    {
        public AlarmUserControl()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 注册警报集合属性
        /// </summary>
        public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(DependencyObject), new PropertyMetadata(defaultValue: null, propertyChangedCallback: null, coerceValueCallback: OnItemsSourcePropertyChanged));
        public DependencyObject ItemsSource
        {
            get
            {
                return (DependencyObject)GetValue(ItemsSourceProperty);
            }
            set
            {
                SetValue(ItemsSourceProperty, value);
            }
        }
        /// <summary>
        /// 注册服务器在线状态属性 数据类型为bool
        /// </summary>
        public static readonly DependencyProperty ServerConnectionStatusProperty = DependencyProperty.Register("ServerConnectionStatus", typeof(bool), typeof(DependencyObject), new PropertyMetadata(defaultValue: false, propertyChangedCallback: null, coerceValueCallback: ServerConnectionStatusPropertyChanged));
        public DependencyObject ServerConnectionStatus
        {
            get
            {
                return (DependencyObject)GetValue(ServerConnectionStatusProperty);
            }
            set
            {
                SetValue(ServerConnectionStatusProperty, value);
            }
        }

        public static readonly DependencyProperty CommandProperty =
           DependencyProperty.Register("Command", typeof(ICommand), typeof(AlarmUserControl),
               new PropertyMetadata((ICommand)null, new PropertyChangedCallback(CommandChanged)));

        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.Register("CommandParameter", typeof(object), typeof(AlarmUserControl));

        public static readonly DependencyProperty CommandTargetProperty =
            DependencyProperty.Register("CommandTarget", typeof(IInputElement), typeof(AlarmUserControl));

        private static void CommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            AlarmUserControl control = d as AlarmUserControl;
            if (control != null)
            {
                ICommand oldCommand = e.OldValue as ICommand;
                ICommand newCommand = e.NewValue as ICommand;
                control.UpdateCommand(oldCommand, newCommand);
            }
        }

        private void UpdateCommand(ICommand oldCommand, ICommand newCommand)
        {
            if (oldCommand != null)
            {
                oldCommand.CanExecuteChanged -= CanExecuteChanged;
            }
            if (newCommand != null)
            {
                newCommand.CanExecuteChanged += CanExecuteChanged;
            }
        }

        private void CanExecuteChanged(object sender, EventArgs e)
        {
            RoutedCommand command = this.Command as RoutedCommand;
            if (command != null)
            {
                this.IsEnabled = command.CanExecute(CommandParameter, CommandTarget);
            }
            else if (this.Command != null)
            {
                this.IsEnabled = this.Command.CanExecute(CommandParameter);
            }
        }

        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }

        public object CommandParameter
        {
            get { return GetValue(CommandParameterProperty); }
            set { SetValue(CommandParameterProperty, value); }
        }

        public IInputElement CommandTarget
        {
            get { return (IInputElement)GetValue(CommandTargetProperty); }
            set { SetValue(CommandTargetProperty, value); }
        }

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            RoutedCommand command = Command as RoutedCommand;
            if (command != null)
                command.Execute(CommandParameter, CommandTarget);
            else if (Command != null)
                this.Command.Execute(CommandParameter);
        }


        private static object OnItemsSourcePropertyChanged(DependencyObject d, object baseValue)
        {
            if (baseValue != null)
            {
                AlarmUserControl alarmUserControl = (AlarmUserControl)d;
                alarmUserControl.ListView_Main.ItemsSource = (IEnumerable)baseValue;
                ObservableCollection<AlarmModel> models = (ObservableCollection<AlarmModel>)baseValue;
                alarmUserControl.TextBlock_Total.Text = models.Count().ToString();
                int level1AlarmCount = 0;
                int levelIIAlarmCount = 0;
                int levelIIIAlarmCount = 0;
                for (int i = 0; i < models.Count(); i++)
                {
                    if (models[i].AlarmLevel == "一级报警")
                    {
                        level1AlarmCount++;
                    }
                    else if (models[i].AlarmLevel == "二级报警")
                    {
                        levelIIAlarmCount++;
                    }
                    else if (models[i].AlarmLevel == "三级报警")
                    {
                        levelIIIAlarmCount++;
                    }
                }
                alarmUserControl.TextBlock_Level1AlarmCount.Text = level1AlarmCount.ToString();
                alarmUserControl.TextBlock_LevelIIAlarmCount.Text = levelIIAlarmCount.ToString();
                alarmUserControl.TextBlock_LevelIIIAlarmCount.Text = levelIIIAlarmCount.ToString();
                IEnumerable alarms = (IEnumerable)baseValue;
                object last = null;
                foreach (var item in alarms)
                {
                    last = item;
                }
                if (last != null)
                {
                    alarmUserControl.ListView_Main.ScrollIntoView(last);
                }
            }
            return baseValue;
        }
        private static object ServerConnectionStatusPropertyChanged(DependencyObject d, object baseValue)
        {
            Boolean oldValue = (Boolean)baseValue;
            AlarmUserControl alarmUserControl = (AlarmUserControl)d;
            if (oldValue)
            {
                alarmUserControl.Image_ServerConnectionStatus.Source = new BitmapImage(new Uri("/View/UserControls/server_OnLine.png", UriKind.Relative));
                alarmUserControl.Image_ServerConnectionStatus.ToolTip = "与报警服务器网络通信正常！";
            }
            else
            {
                alarmUserControl.Image_ServerConnectionStatus.Source = new BitmapImage(new Uri("/View/UserControls/server_Dropped.png", UriKind.Relative));
                alarmUserControl.Image_ServerConnectionStatus.ToolTip = "与报警服务器网络通信异常，请检查网络状况！";
            }
            return baseValue;
        }

        private void ListView_Main_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            RoutedCommand command = Command as RoutedCommand;
            if (command != null)
                command.Execute(CommandParameter, CommandTarget);
            else if (Command != null)
                this.Command.Execute(CommandParameter);
        }




        ///// <summary>
        ///// 重置
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void Button_Reset_Click(object sender, RoutedEventArgs e)
        //{
        //    this.ListView_Main.ItemsSource = null;
        //    this.ListView_Main.ItemsSource = (ObservableCollection<AlarmModel>)ItemsSource;

        //}
    }
    /// <summary>
    /// 报警显示模型
    /// </summary>
    public class AlarmModel
    {
        /// <summary>
        /// 序号
        /// </summary>
        private int _OrderNo;
        /// <summary>
        /// 序号
        /// </summary>
        public int OrderNo
        {
            set { _OrderNo = value; }
            get { return _OrderNo; }
        }
        /// <summary>
        /// 报警等级
        /// </summary>
        private string _AlarmLevel;
        /// <summary>
        /// 报警等级
        /// </summary>
        public string AlarmLevel
        {
            get { return _AlarmLevel; }
            set
            {
                _AlarmLevel = value;
            }
        }
        /// <summary>
        /// 报警等级图片地址
        /// </summary>
        public string AlarmLevelImageUrl
        {
            get
            {
                if (AlarmLevel == "一级报警")
                {
                    return "/View/UserControls/Level1Alarm.png";
                }
                else if (AlarmLevel == "二级报警")
                {
                    return "/View/UserControls/LevelIIAlarm.png";
                }
                else
                {
                    return "/View/UserControls/LevelIIIAlarm.png";
                }
            }
        }
        /// <summary>
        /// 报警内容
        /// </summary>
        private string _AlarmContent;
        /// <summary>
        /// 报警内容
        /// </summary>
        public string AlarmContent
        {
            set { _AlarmContent = value; }
            get { return _AlarmContent; }
        }
        /// <summary>
        /// 报警时间
        /// </summary>
        private DateTime _AlarmDateTime;
        /// <summary>
        /// 报警时间
        /// </summary>
        public DateTime AlarmDateTime
        {
            get { return _AlarmDateTime; }
            set { _AlarmDateTime = value; }
        }
        /// <summary>
        /// 报警时间字符串
        /// </summary>
        public string AlarmDateTimeStr
        {
            get { return AlarmDateTime.ToString("yyyy-MM-dd HH:mm:ss"); }
        }
    }
}
